/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package tests.flexunit.comparers
{
	import flexunit.framework.AssertionFailedError;
	import flexunit.framework.TestCase;
	
	import funit.resources.AssertResources;
	
	import sv.data.comparers.StringComparer;
	import sv.utils.ClassUtil;
	
	public class StringComparerTests extends TestCase
	{
		private var stringComparer:StringComparer;
		private var stringComparerIgnoreCase:StringComparer;
		
		
		public function StringComparerTests( methodName:String = null )
		{
			super(methodName);
		}
		
		
		override public function setUp() : void
		{
			stringComparer = new StringComparer();
			stringComparerIgnoreCase = new StringComparer(true);
		}
		
		///////////////////////////////////////////
		// Compare Tests
		///////////////////////////////////////////
		
		public function testCompareHandleNulls() : void
		{
			var string1:String = "123456";
			
			assertEquals( 0, stringComparer.compare(null, null) );
			assertEquals( 0, stringComparerIgnoreCase.compare(null, null) );
			
			assertEquals(-1, stringComparer.compare(null, string1) );
			assertEquals(-1, stringComparerIgnoreCase.compare(null, string1) );
			
			assertEquals( 1, stringComparer.compare(string1, null) );
			assertEquals( 1, stringComparerIgnoreCase.compare(string1, null) );
		}
		
		public function testCompareHandleEmpty() : void
		{
			var string1:String = "123456";
			
			assertEquals( 0, stringComparer.compare("", "") );
			assertEquals( 0, stringComparerIgnoreCase.compare("", "") );
			
			assertEquals(-1, stringComparer.compare("", string1) );
			assertEquals(-1, stringComparerIgnoreCase.compare("", string1) );
			
			assertEquals( 1, stringComparer.compare(string1, "") );
			assertEquals( 1, stringComparerIgnoreCase.compare(string1, "") );
		}
		
		public function testCompareHandleAreSame() : void
		{
			var string1:String = "123456";
			
			assertEquals( 0, stringComparer.compare(string1, string1) );
		}
		
		public function testCompare() : void
		{
			var string1:String = "123456";
			var string2:String = "123456";
			var string3:String = "654321";
			
			assertEquals( 0, stringComparer.compare(string1, string2) );
			
			assertEquals(-1, stringComparer.compare(string2, string3) );
			assertEquals( 1, stringComparer.compare(string3, string2) );
		}
		
		public function testCompareFailureOnTypeMismatch() : void
		{
			var string1:String = "123456";
			var number:Number = 123456;
			
			try
			{
				stringComparer.compare(string1, number);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		public function testCompareFailureOnTypeMismatch2() : void
		{
			var string1:String = "123456";
			var number:Number = 123456;
			
			try
			{
				stringComparer.compare(number, string1);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		///////////////////////////////////////////
		// Equals Tests
		///////////////////////////////////////////
		
		public function testEqualsHandleNulls() : void
		{
			var string1:String = "123456";
			
			assertTrue( stringComparer.equals(null, null) );
			
			assertFalse( stringComparer.equals(null, string1) );
			assertFalse( stringComparer.equals(string1, null) );
		}
		
		public function testEqualsHandleEmpty() : void
		{
			var string1:String = "123456";
			
			assertTrue( stringComparer.equals("", "") );
			
			assertFalse( stringComparer.equals("", string1) );
			assertFalse( stringComparer.equals(string1, "") );
		}
		
		public function testEqualsHandleAreSame() : void
		{
			var string1:String = "123456";
			
			assertTrue( stringComparer.equals(string1, string1) );
		}
		
		public function testEquals() : void
		{
			var string1:String = "123456";
			var string2:String = "123456";
			var string3:String = "654321";
			
			assertTrue( stringComparer.equals(string1, string2) );
			assertTrue( stringComparerIgnoreCase.equals(string1, string2) );
			
			assertFalse( stringComparer.equals(string2, string3) );
			assertFalse( stringComparer.equals(string3, string2) );
			assertFalse( stringComparerIgnoreCase.equals(string2, string3) );
			assertFalse( stringComparerIgnoreCase.equals(string3, string2) );
		}
		
		public function testEqualsIgnoringCase() : void
		{
			var string1:String = "Hello World!";
			var string2:String = "hello world!";
			var string3:String = "HELLO world!";
			var string4:String = "goodbye world!";
			
			assertTrue( stringComparerIgnoreCase.equals(string1, string2) );
			assertTrue( stringComparerIgnoreCase.equals(string1, string3) );
			
			assertFalse( stringComparerIgnoreCase.equals(string1, string4) );
		}
		
		public function testEqualsFailureOnTypeMismatch() : void
		{
			var string1:String = "123456";
			var number:Number = 123456;
			
			try
			{
				stringComparer.equals(string1, number);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		public function testEqualsFailureOnTypeMismatch2() : void
		{
			var string1:String = "123456";
			var number:Number = 123456;
			
			try
			{
				stringComparer.equals(number, string1);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
	}
	
}